package com.lhh.userservice.controller;

import com.lhh.springcommon.annotation.WebLog;
import com.lhh.springcommon.annotation.WithRequestId;
import com.lhh.springcommon.entity.User;
import com.lhh.springcommon.entity.vo.UserVO;
import com.lhh.springcommon.utils.Response;
import com.lhh.userservice.service.UserManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户账号管理
 * @author 李厚航
 * @date 2025-02-25
 */
@RestController
@RequestMapping("/userManagement")
public class UserManagementController {

    private final UserManagementService userManagementService;

    @Autowired
    public UserManagementController(UserManagementService userManagementService) {
        this.userManagementService = userManagementService;
    }

    /**
     * 用户账号密码登录
     * @param user 用户数据
     * @return token
     */
    @PostMapping("/login")
    @WithRequestId
    @WebLog
    public Response<String> login(@RequestBody User user){
        return Response.success(userManagementService.login(user));
    }

    /**
     * 用户短信验证码登录
     * @param phoneNumber 手机号
     * @param code 验证码
     * @return
     */
    @GetMapping("/smsLogin")
    @WithRequestId
    @WebLog
    public Response<String> smsLogin(@RequestParam("phone_number") String phoneNumber, @RequestParam("code") String code){
        return Response.success(userManagementService.smsLogin(phoneNumber, code));
    }

    /**
     * 用户注册
     * @param user 用户注册数据
     * @return 用户信息
     */
    @PostMapping("/register")
    @WithRequestId
    @WebLog
    public Response<UserVO> register(@RequestBody User user){
        return Response.success(userManagementService.register(user));
    }

    /**
     * 根据手机号发送短信验证码
     * @param phoneNumber 手机号
     * @return 短信验证码
     */
    @GetMapping("/sms")
    @WithRequestId
    @WebLog
    public Response<String> sms(@RequestParam("phone_number") String phoneNumber) throws Exception {
        userManagementService.sms(phoneNumber);
        return Response.success(null);
    }

    /**
     * 根据手机号用户登录
     * @param phoneNumber 手机号
     * @return 返回的用户信息
     */
    @GetMapping("/login")
    @WithRequestId
    @WebLog
    public Response<String> login(@RequestParam("phone_number") String phoneNumber, @RequestParam("code") String code){
        return Response.success(userManagementService.smsLogin(phoneNumber, code));
    }

    /**
     * 判断用户账号，用户名，手机号可以注册
     * @param key 判断的对象
     * @param value 判断的值
     * @return true为不重复，false为重复
     */
    @GetMapping("/checkNoRepeat")
    @WithRequestId
    @WebLog
    public Response<Boolean> checkNoRepeat(@RequestParam("key") String key,@RequestParam("value") String value) {
        return Response.success(userManagementService.checkNoRepeat(key, value));
    }

}

